
<template>
  <a-card :bordered="false" :bodyStyle="{ padding: '16px', height: '100%' }">
    <div class="table-page-search-wrapper" style="padding-left:10px;padding-right:10px;">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="搜索课程">
              <a-input
                v-model="keywords"
                placeholder="请输入课程名称(回车搜索)"
                @pressEnter="$refs.table.refresh(true)"
              >
                <a-tooltip slot="suffix" title="请输入课程名称">
                  <a-icon
                    type="search"
                    style="color: rgba(0,0,0,.45)"
                    @click="$refs.table.refresh(true)"
                  />
                </a-tooltip>
              </a-input>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="8" :sm="24">
            <a-form-item label="商品名称">
              <a-input v-model="queryParam.name" placeholder="请选择商品名称" />
            </a-form-item>
          </a-col>-->
          <a-col :md="5" :sm="24">
            <a-form-item label="课程类型">
              <!-- <a-radio-group v-model="queryParam.type">
                <a-radio :value="1">一对一</a-radio>
                <a-radio :value="2">一对多</a-radio>
              </a-radio-group>-->
              <a-checkbox @change="openEnable" :checked="openChecked">一对一</a-checkbox>
              <a-checkbox @change="closeEnable" :checked="closeChecked">一对多</a-checkbox>
            </a-form-item>
          </a-col>
          <!-- <template v-if="advanced"> -->
          <a-col :md="5" :sm="24">
            <a-form-item label="课程状态">
              <!-- <a-radio-group v-model="queryParam.is_enable">
                <a-radio :value="1">启用</a-radio>
                <a-radio :value="2">禁用</a-radio>
              </a-radio-group>-->
              <a-checkbox @change="openEnable1" :checked="endable">启用</a-checkbox>
              <a-checkbox @change="closeEnable1" :checked="disable">禁用</a-checkbox>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="8" :sm="24">
              <a-form-item label="线上售卖">
                <a-select v-model="queryParam.useStatus" placeholder="请选择" default-value="0">
                  <a-select-option value="0">全部</a-select-option>
                  <a-select-option value="1">关闭</a-select-option>
                  <a-select-option value="2">运行中</a-select-option>
                </a-select>
              </a-form-item>
          </a-col>-->
          <!-- </template> -->
          <!-- <a-col :md="!advanced && 8 || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              :style="advanced && { float: 'right', overflow: 'hidden' } || {} "
            >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">清空搜索条件</a-button>
              <a @click="toggleAdvanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>-->
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <router-link :to="{name:'course_create'}">
        <a-button type="primary" style="margin-right:20px;">新建课程</a-button>
      </router-link>
      <!-- <a-button>购买商城二维码</a-button> -->
    </div>
    <!-- <a-alert type="warning" message="物品的售卖详情可在“财务-收据明细”中查看" banner class="expand_warning" /> -->
    <s-table
      :columns="columns"
      :data="loadData"
      class="user_management_table"
      style="margin-top:20px;"
      ref="table"
      :rowKey="(record) => record.id"
      :pagination="pagination"
      :scroll="{ x: 1500}"
    >
      <span slot="is_enable" slot-scope="text,record">
        <a-switch :checked="text==1?true:false" @click="changeType(text,record)" />
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <!-- <a @click="handleEdit(record)">线上售卖</a>
          <a-divider type="vertical" />-->
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />

          <a @click="del(record)">删除</a>
        </template>
      </span>
      <div slot="money" slot-scope="text,record">
        <div class="money" v-if="record.courseTimeArr.length>0">
          <div class="chargeWay">按课时收费</div>
          <div class="chargeStandard">
            <div v-for="(item,index) in record.courseTimeArr" :key="index">
              <div class="price" v-if="Number(item.number)==1">{{item.name}}({{item.total_price||'0'}}元/课时)</div>
              <div class="price" v-else>{{item.name}}({{item.total_price||'0'}}元{{item.number}}课时)</div>
            </div>
          </div>
        </div>
        <div class="money" v-if="record.dayArr.length>0">
          <div class="chargeWay">按天收费</div>
          <div class="chargeStandard">
            <div v-for="(item,index) in record.dayArr" :key="index">
              <!-- {{item}} -->
              <div class="price" v-if="Number(item.number)==1">{{item.name}}({{item.total_price||'0'}}元/天)</div>
              <div class="price" v-else>{{item.name}}({{item.total_price||'0'}}元{{item.number}}天)</div>
            </div>
          </div>
        </div>
      </div>
    </s-table>
    <create-form ref="createModal" @ok="handleOk" />
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import CreateForm from '../../list/modules/CreateForm'
import { coursesList, coursesDel, changeCoursesType } from '@/api/courseCharge'
export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    CreateForm
  },
  data() {
    return {
      openChecked: false,
      closeChecked: false,
      endable: false,
      disable: false,
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      keywords: '',
      queryParam: {
        is_enable: null,
        type: '',
        school_id: this.$store.state.user.school
      },
      // 表头
      columns: [
        {
          title: '课程名称',
          dataIndex: 'name',
          width: 200,
          fixed: 'left',
          ellipsis: true,
        },
        {
          title: '类型',
          dataIndex: 'type',
          width: 200,
          ellipsis: true,
          align: 'center',
          customRender(type) {
            return type == 1 ? '一对一' : '一对多' //1是男，2是女
          }
        },
        {
          title: '在读学员数',
          dataIndex: 'student_num',
          width: 200,
          ellipsis: true,
          align: 'center'
        },
        {
          title: '收费方式/定价标准',
          dataIndex: 'money',
          ellipsis: true,
          scopedSlots: { customRender: 'money' }
        },
        {
          title: '启用状态',
          dataIndex: 'is_enable',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'is_enable' }
        },
        {
          title: '操作',
          dataIndex: 'updatedAt',
          fixed: 'right',
          width: 100,
          align: 'right',
          ellipsis: true,
          scopedSlots: { customRender: 'action' }
        }
      ],
      moreColumns: [
        {
          title: '单价',
          dataIndex: 'add',
          align: 'center' //  scopedSlots: { customRender: 'actions' },
        },
        {
          title: '定价标准',
          dataIndex: 'type',
          align: 'center'
        }
      ],
      coursePageNo:1,
      pagination:{
        showTotal:total=>`共${total}条数据`,
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return coursesList(Object.assign(parameter, this.queryParam, { keywords: this.keywords })).then(res => {
          let params = JSON.parse(JSON.stringify(res))
          params.data.forEach(val => {
            if (val.course_price) {
              if (val.course_price.data) {
                val.courseTimeArr = []
                val.dayArr = []
                val.course_price.data.forEach((item, index) => {
                  if (item.type == 1) {
                    item.add.forEach((j, k) => {
                      val.courseTimeArr.push(j)
                    })
                  } else if (item.type == 2) {
                    item.add.forEach((j, k) => {
                      val.dayArr.push(j)
                    })
                  }
                })
              }
            }
          })
          this.coursePageNo=Number(parameter.pageNo)
          return params
        })
      }
    }
  },
  watch: {
    queryParam: {
      handler(val) {
        this.$refs.table.refresh(true)
      },
      deep: true
    }
  },
  created() {},
  methods: {
    changeType(data, e) {
      if (data == 2 || data == null) {
        changeCoursesType({ is_enable: 1 }, e.id)
          .then(res => {
            this.$refs.table.refresh(false)
          })
          .catch(err => {
            this.$message.error(err.data.message || '更新状态失败')
          })
      } else {
        changeCoursesType({ is_enable: 2 }, e.id)
          .then(res => {
            this.$refs.table.refresh(false)
          })
          .catch(err => {
            this.$message.error(err.data.message || '更新状态失败')
          })
      }
    },
    del(e) {
      let that = this
      this.$confirm({
        title: '提示',
        content: '确认删除课程：' + e.name + '？',
        okText: '确定',
        okType: 'primary',
        cancelText: '取消',
        onOk() {
          coursesDel(e.id)
            .then(res => {
              that.$message.success('删除成功')
              that.$refs.table.refresh(false)
            })
            .catch(err => {
              that.$message.error(err.data.message || '删除失败')
            })
        },
        onCancel() {
          console.log('Cancel')
        }
      })
    },
    handleEdit(record) {
       sessionStorage.setItem("sessionPageNo", JSON.stringify({coursePageNo:this.coursePageNo}));
      this.$router.push({ name: 'course_edit', query: { id: record.id } })
    },
    handleSub(record) {
      if (record.status !== 0) {
        this.$message.info(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    openEnable(e) {
      if (e.target.checked == true) {
        this.closeChecked = false
        this.queryParam.type = 1
        this.openChecked = true
      } else {
        this.queryParam.type = null
        this.openChecked = false
      }
    },
    closeEnable(e) {
      if (e.target.checked == true) {
        this.openChecked = false
        this.queryParam.type = 2
        this.closeChecked = true
      } else {
        this.queryParam.type = null
        this.closeChecked = false
      }
    },
    openEnable1(e) {
      if (e.target.checked == true) {
        this.disable = false
        this.queryParam.is_enable = 1
        this.endable = true
      } else {
        this.queryParam.is_enable = null
        this.endable = false
      }
    },
    closeEnable1(e) {
      if (e.target.checked == true) {
        this.endable = false
        this.queryParam.is_enable = 2
        this.disable = true
      } else {
        this.queryParam.is_enable = null
        this.disable = false
      }
    }
  }
}
</script>
<style lang="less" scoped>
.expand_warning {
  margin: 16px 0;
  background-color: #fff0e7;
  border-radius: 4px;
}
.money {
  display: flex;
  align-items: center;
  color: #666;
  border-top: 1px dashed #e8e8e8;
  .chargeWay {
    width: 80px;
  }
  // justify-content: space-between;
  .chargeStandard {
    margin-left: 30px;
    .price {
      line-height: 30px;
      white-space: pre-wrap;
    }
  }
}
.money:first-child {
  border-top: 0px;
}
</style>